{% extends "index/main.html" -%}
{% block content -%}
{% set id = bo.id | default(value='') -%}
<form id="forms" action="{{ctx}}/market/saveRoute.html" method="post" onsubmit="return submitForm();">
	<div class="row">
		<div class="w5 group-form">
			<label class="group-label">出乘日期</label>
			<div class="group-content">
				<input type="hidden" id="json" name="json" value="">
				<input type="text" id="workDate" rule="txt:出乘日期;required" value="{{bo.workDate | default(value='')}}" class="iceDate-icon">
			</div>
		</div>
	</div>
	<div class="row">
		<div class="w5 group-form">
			<table class="table table-border table-responsive">
				<tr>
					<th>车次</th>
					<th>销售人员</th>
				</tr>
				{% if id=="" -%}
				{% for me in trains -%}
				<tr>
					<td>
						<input type="hidden" id="days" value="{{me.days}}">
						<input type="text" id="trains" value="{{me.name}}" readonly>
					</td>
					<td>
						<input type="hidden" id="userId_{{me.name}}" value="">
						<input type="hidden" id="name_{{me.name}}" value="">
						<select id="{{me.name}}" change="setUsers" class="sel">
							<option value="">请选择</option>
							{% for me in users -%}
							<option value="{{me.id}}">{{me.name}}</option>
							{% endfor -%}
						</select>
					</td>
				</tr>
				{% endfor -%}
				{% else -%}
				<tr>
					<td>
						<input type="hidden" id="id" value="{{id}}">
						<input type="hidden" id="days" value="{{bo.days}}">
						<input type="text" id="trains" value="{{bo.trains}}" readonly>
					</td>
					<td>
						<input type="hidden" id="userId_{{bo.trains}}" value="{{bo.userId}}">
						<input type="hidden" id="name_{{bo.trains}}" value="{{bo.name}}">
						<select id="{{bo.trains}}" change="setUsers" class="sel">
							<option value="">请选择</option>
							{% for me in users -%}
								{% if me.id == bo.userId -%}
								<option value="{{me.id}}" selected>{{me.name}}</option>
								{% else -%}
								<option value="{{me.id}}">{{me.name}}</option>
								{% endif -%}
							{% endfor -%}
						</select>
					</td>
				</tr>
				{% endif -%}
			</table>
		</div>
	</div>
	<div class="row">
		<div class="w5 group-form">
			<button type="submit" class="btn bg-royal" style="margin-left: 25rem;">提交</button>
			<a href="{{ctx}}/market/routeList.html" class="btn bg-royal">返回</a>
		</div>
	</div>
</form>
<script type="text/javascript">
	ice(function() {
		iceui.use('date', function() {
			ice.date({
				id : 'workDate',
				format : 'y-m-d'
			});
		});
	});

	function setUsers(id, name, obj) {
		var num = obj.id;
		ice('#userId_' + num).val(id);
		ice('#name_' + num).val(name);
	}

	function submitForm() {
		if (!iceui.validateForm('forms')) {
			return false;
		}

		var id = ice('#id').val();
		var workDate = ice('#workDate').val();
		if (id) {
			var trains = ice('#trains').val();
			var days = ice('#days').val();
			var userId = ice('input[id^="userId"]')[0].value;
			var name = ice('input[id^="name"]')[0].value;
			var quitDate = new Date(workDate);
			quitDate.setDate(quitDate.getDate() + Math.ceil(days) - 1);
			var list = [ {
				'id' : id,
				'userId' : userId,
				'name' : name,
				'trains' : trains,
				'days' : days,
				'workDate' : workDate,
				'quitDate' : ice.formatDate(quitDate.getTime(), 'y-m-d')
			} ];
			ice('#json').val(JSON.stringify(list));
		} else {
			var trains = ice('input[id="trains"]');
			var days = ice('input[id="days"]');
			var userId = ice('input[id^="userId"]');
			var name = ice('input[id^="name"]');
			var length = name.length;
			var list = new Array(length);
			var quitDate = null, day = '';
			for (var i = 0; i < length; ++i) {
				day = days[i].value;
				quitDate = new Date(workDate);
				quitDate.setDate(quitDate.getDate() + Math.ceil(day) - 1);
				list[i] = {
					'userId' : userId[i].value,
					'name' : name[i].value,
					'trains' : trains[i].value,
					'days' : day,
					'workDate' : workDate,
					'quitDate' : ice.formatDate(quitDate.getTime(), 'y-m-d')
				};
			}
			ice('#json').val(JSON.stringify(list));
		}

		return true;
	}
</script>
{% endblock content -%}
